package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patchmanage.back.annotation.LoginValidate;
import com.patchmanage.back.annotation.OperLog;
import com.patchmanage.back.entity.DepartmentGroup;
import com.patchmanage.back.entity.InventoryGroup;
import com.patchmanage.back.result.Result;
import com.patchmanage.back.service.IDepartmentGroupService;
import com.patchmanage.back.service.IInventoryGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

/**
 * <p>
 *  资产规则组 控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-03-20
 */
@Api(value = " 资产组 controller", tags = {"资产组 Api"})
@RestController
@RequestMapping("/back/inventory-group")
public class InventoryGroupController {

    @Autowired
    public IInventoryGroupService inventoryGroupService;

    @Autowired
    public IDepartmentGroupService departmentGroupService;

    /**
     * 获取资产组列表
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取资产组列表接口", notes = "获取资产组列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", required = false, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "groupId", value = "groupId", required = false, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", required = true, dataType = "Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", required = true, dataType = "Integer", paramType = "body")
    })
    public Result<IPage<InventoryGroup>> list(@ApiIgnore  @RequestBody Dict dict) {

        Integer pageNo = dict.getInt("pageNo");
        Integer pageSize = dict.getInt("pageSize");
        String name = dict.getStr("name");
        Integer groupId = dict.getInt("groupId");

        Page<InventoryGroup> page = new Page(pageNo, pageSize);
        QueryWrapper<InventoryGroup> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        if(groupId != null){
            queryWrapper.like("group_id",groupId);
        }
        return Result.success(inventoryGroupService.page(page,queryWrapper));
    }


    /**
     * 获取资产组信息
     * @param dict
     * @return
     */
    @ApiOperation(value = "获取资产组信息接口", notes = "获取资产组信息接口")
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body"),
    })
    @LoginValidate
    public Result<InventoryGroup> getById(@ApiIgnore @RequestBody Dict dict) {
        return Result.success(inventoryGroupService.getById(dict.getInt("id")));
    }

    /**
     * 开启或关闭规则
     * @param dict
     * @return
     */
    @ApiOperation(value = "开启或关闭规则接口(策略状态(0:关闭 1:开启))", notes = "开启或关闭规则接口(策略状态(0:关闭 1:开启))")
    @RequestMapping(value = "setPolicyStatus", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "policyStatus", value = "policyStatus", required = true, dataType = "Integer", paramType = "body"),
    })
    @LoginValidate
    public Result<Boolean> setPolicyStatus(@ApiIgnore @RequestBody Dict dict) {

        String id = dict.getStr("id");
        Integer policyStatus = dict.getInt("policyStatus");
        String[] ids = id.split(",");


        InventoryGroup inventoryGroup = new InventoryGroup();
        inventoryGroup.setPolicyStatus(policyStatus);


        UpdateWrapper<InventoryGroup> updateWrapper = new UpdateWrapper();
        updateWrapper.in("id",Arrays.asList(ids));

        return Result.success(inventoryGroupService.update(inventoryGroup, updateWrapper));
    }


    /**
     * 新增或修改资产组信息
     * @param inventoryGroup
     * @return
     */
    @ApiOperation(value = "新增或修改资产组接口", notes = "新增或修改资产组接口")
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @LoginValidate
    @OperLog(operModul="资产组",operDesc="新增或修改资产组")
    public Result<Boolean> saveOrUpdate(@RequestBody InventoryGroup inventoryGroup) {

        if(inventoryGroup.getGroupId() != null){
            DepartmentGroup group = departmentGroupService.getById(inventoryGroup.getGroupId());
            inventoryGroup.setGroupName(group.getName());
        }
        return Result.success(inventoryGroupService.saveOrUpdate(inventoryGroup));
    }

    /**
     * 删除资产组
     * @param dict
     * @return
     */
    @ApiOperation(value = "删除资产组接口", notes = "删除资产组接口")
    @RequestMapping(value = "removeById", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "body"),
    })
    @OperLog(operModul="资产组",operDesc="删除补丁")
    public Result<Boolean> removeById(@ApiIgnore @RequestBody Dict dict) {
        String id = dict.getStr("id");
        String[] ids = id.split(",");
        return Result.success(inventoryGroupService.removeByIds(Arrays.asList(ids)));
    }
}
